//////////////////////////////////////////////////////////////////////////////
//
//  Created by Philip Mulcahy on 30/01/2012.
//  Copyright (c) 2012 Philip Mulcahy. All rights reserved.
//
//  This file is part of the note-recog library.
//
//  note-recog is free software: you can redistribute it and/or modify
//  it under the terms of version 3 of the GNU Lesser General Public License 
//  as published by the Free Software Foundation.
//
//  note-recog is distributed is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with note-recog.  If not, see <http://www.gnu.org/licenses/>.
//
//////////////////////////////////////////////////////////////////////////////

#ifndef note_recog_Condenser_h
#define note_recog_Condenser_h

#include <iostream>
#include "Log.h"
#include "EdgeDetector.h"
#include "PitchDetector.h"
#include "IEventListener.h"

class IConsumer;
class Slice;

class Condenser
{
public:
    class InitInfo {
    private:
        float _transientBlackoutDuration;
        float _minQuality;
        float _decayTimeConstant;    
    public:
        InitInfo();
        const float& transientBlackoutDuration() const;
        const float& minQuality() const;
        const float& decayTimeConstant() const;
        void setTransientBlackoutDuration(const float&);
        void setMinQuality(const float&);
        void setDecayTimeConstant(const float&);
    };
private:
    friend std::ostream& operator<<(std::ostream&, const Condenser&);
    LOG_DECLARE;
    
    NullListener<EdgeDetector::Event> _edge_listener;
    NullListener<PitchDetector::Event> _pitch_listener;
    
    IConsumer * const _consumer;
    
    float _lastRms;
    float _decayingRmsEnvelope;
    double _lastTime; //seconds
    double _lastRiseTime;
    double _lastNoteSentTime;
    size_t _eventCount;
    const InitInfo _initInfo;
    EdgeDetector * const _edgeDetector;
    PitchDetector * const _pitchDetector;
    
public: 
    Condenser( IConsumer * consumer );
    Condenser( IConsumer * consumer, const InitInfo& );
    void submit( const Slice& slice );
};

std::ostream& operator<<(std::ostream&, const Condenser&);

#endif
